package services

import (
	"GOPAAS/internal/app/model"
	"GOPAAS/internal/utils"
)

type ConsultService struct {
}

func GetConsultService() *ConsultService {
	return &ConsultService{}
}

type ConsultResult struct {
	Id           int64  `json:"id"`
	Name         string `json:"name"`
	PartnerName  string `json:"partner_name"`
	TicketTypeId string `json:"ticket_type_id"`
	PartnerPhone string `json:"partner_phone"`
	PartnerEmail string `json:"partner_email"`
	StageId      string `json:"stage_id"`
	CreateDate   string `json:"create_date"`
	Description  string `json:"description"`
}

func (c ConsultService) GetList(search string, start int, length int, order string, sort string, uid int64, state string) (res []ConsultResult, total int64, err error) {
	db = getDB()
	res = make([]ConsultResult, 0)
	data_query := db.Model(&model.HelpdeskTicket{}).Where("create_uid=? and active=?", uid, true)
	count_query := db.Model(&model.HelpdeskTicket{}).Where("create_uid=? and active=?", uid, true)
	if search != "" {
		data_query.Where("name like ?", "%"+search+"%")
		count_query.Where("name like ?", "%"+search+"%")
	}
	if state != "" {
		data_query.Where("stage_id= ?", state)
		count_query.Where("stage_id= ?", state)
	}
	var consult_str = order + " " + sort
	var result []model.HelpdeskTicket
	count_query.Count(&total)
	data_query.Preload("TicketType").Preload("Stage").Offset(start).Limit(length).Order(consult_str).Find(&result)
	for _, v := range result {
		res = append(res, ConsultResult{
			Id:           v.Id,
			Name:         v.Name,
			PartnerName:  v.PartnerName,
			PartnerPhone: v.PartnerPhone,
			PartnerEmail: v.PartnerEmail,
			TicketTypeId: v.TicketType.Name,
			StageId:      v.Stage.Name,
			CreateDate:   v.CreateDate.Format("2006-01-02 15:04:05"),
		})
	}
	return
}

// Detail 咨询详情
func (c ConsultService) Detail(id int64) (result ConsultResult, err error) {
	db = getDB()
	var consult model.HelpdeskTicket
	db.Preload("TicketType").Preload("Stage").First(&consult, id)
	result = ConsultResult{
		Id:           consult.Id,
		Name:         consult.Name,
		PartnerName:  consult.PartnerName,
		PartnerPhone: consult.PartnerPhone,
		PartnerEmail: consult.PartnerEmail,
		TicketTypeId: consult.TicketType.Name,
		StageId:      consult.Stage.Name,
		CreateDate:   consult.CreateDate.Format("2006-01-02 15:04:05"),
	}
	return
}

// Create 创建咨询
func (c ConsultService) Create(user_id int64, name string, description string, ticket_type_id int64, email string, phone string) (ticket_id int64, err error) {
	var helpdesk model.HelpdeskTicket
	db = getDB()
	var user model.ResUsers
	db.Preload("Partner").First(&user, user_id)
	helpdesk = model.HelpdeskTicket{
		Name:         name,
		Description:  description,
		TicketTypeId: utils.NullInt64Value(ticket_type_id),
		UserId:       2,
		PartnerId:    user.PartnerId,
		PartnerName:  user.Partner.Name,
		PartnerEmail: email,
		PartnerPhone: phone,
		StageId:      c.GetFirstStageId(),
		KanbanState:  "normal",
		Active:       true,
		ResCommon: model.ResCommon{
			CreateUid: user_id,
			WriteUid:  user_id,
		},
	}
	db.Select("KanbanState", "active", "Name", "Description", "TicketTypeId", "UserId", "PartnerId", "PartnerName", "PartnerEmail", "PartnerPhone", "StageId", "CreateUid", "WriteUid").Create(&helpdesk)
	ticket_id = helpdesk.Id
	err = db.Error
	return
}

// GetFirstStageId 获取第一步状态id
func (c ConsultService) GetFirstStageId() int64 {
	db = getDB()
	var stage model.HelpdeskStage
	db.Where("active=?", true).Order("sequence").First(&stage)
	return stage.Id
}
